package com.grid.action;
import java.io.StringReader;
import java.net.URI;
import java.sql.*;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

import org.apache.struts2.ServletActionContext;


import com.opensymphony.xwork2.ActionContext;
import com.grid.model.Application;
import com.grid.model.Applications;
import com.grid.model.Operation;
import com.grid.model.Operations;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.ClientResponse.Status;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class GetOperationDetails extends ActionSupport {

	private String SelectedApplication;
	
	public String getSelectedApplication() {
		return SelectedApplication;
	}

	public void setSelectedApplication(String selectedApplication) {
		SelectedApplication = selectedApplication;
	}

	
	Map<String,Integer> OperationNameMapping = new LinkedHashMap<String, Integer>();
	
	public Map<String, Integer> getOperationNameMapping() {
		return OperationNameMapping;
	}



	public void setOperationNameMapping(Map<String, Integer> operationNameMapping) {
		OperationNameMapping = operationNameMapping;
	}

	private ArrayList<Operation> OperationList = new ArrayList<Operation>();
	

	public ArrayList<Operation> getOperationList() {
		return OperationList;
	}

	public void setOperationList(ArrayList<Operation> operationList) {
		OperationList = operationList;
	}


	private ArrayList<String> selectedCheckBoxList = new ArrayList<String>();
	
	public ArrayList<String> getSelectedCheckBoxList() {
		return selectedCheckBoxList;
	}

	public void setSelectedCheckBoxList(ArrayList<String> selectedCheckBoxList) {
		this.selectedCheckBoxList = selectedCheckBoxList;
	}


	private ArrayList<String> Overlays = new ArrayList<String>();
	
	public ArrayList<String> getOverlays() {
		return Overlays;
	}

	public void setOverlays(ArrayList<String> overlays) {
		Overlays = overlays;
	}
	
	private String overlay;
	


	public String getOverlay() {
		return overlay;
	}

	public void setOverlay(String overlay) {
		this.overlay = overlay;
	}

	private String checkeditems;
	
	public String getCheckeditems() {
		return checkeditems;
	}

	public void setCheckeditems(String checkeditems) {
		this.checkeditems = checkeditems;
	}

	public String execute(){
	
		Map<String, Object> session = ActionContext.getContext().getSession();
		
		if(SelectedApplication != null)
		{
			session.put("SelectedApplication", SelectedApplication);
		}
		else
		{
			SelectedApplication = session.get("SelectedApplication").toString();
		}
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		checkeditems = request.getParameter("checkeditems");
		
		if(checkeditems != null){
			String[] temparray = checkeditems.split(",");
			
			for(int i=0; i< temparray.length; i++){
				selectedCheckBoxList.add(temparray[i]);
			}
		}
		
		
	System.out.println("in execute of web service client operation");
	ClientConfig config = new DefaultClientConfig();
	Client client = Client.create(config);
	WebResource service = client.resource(getBaseURI());
	ClientResponse response;

		if(SelectedApplication.equals("FirstApplication")){
			if(selectedCheckBoxList.size() == 0)
			{
				
				response = service.path("rest").path("operation").path("application").path(SelectedApplication).get(ClientResponse.class);

			}
			else
			{
				String size = selectedCheckBoxList.size()+"";
				System.out.println("size is" + size);
				response = service.path("rest").path("operation").path("applicationtwo").path(SelectedApplication).path(size).get(ClientResponse.class);
			}
		}
		else
		{
			response = service.path("rest").path("operation").path("applicationone").path(SelectedApplication).get(ClientResponse.class);
		}
		
		String xml = response.getEntity(String.class);
		
		
	    Operations res = new Operations();
	    
	    try
	    {
		    JAXBContext ctx = JAXBContext.newInstance(Operations.class);
	        Unmarshaller um = ctx.createUnmarshaller();
	        res = (Operations)um.unmarshal(new StringReader(xml));
	    }
	    catch(Exception ex){
	    	System.out.println("jaxb exception occured");
	    }
        
	    OperationList = res.getOperationList();
	    
	    for(Operation temp : OperationList){
	    	OperationNameMapping.put(temp.getOperationName(), temp.getOperationValue());
	    }
	    
		
		Overlays.add("Splunk");
		Overlays.add("E2E");
		Overlays.add("EMAN");
		Overlays.add("AWR");
		Overlays.add("Active P1/P2");
		Overlays.add("Data Volumes");
		
		return SUCCESS;
	}
	
	private static URI getBaseURI() {
	    return UriBuilder.fromUri("http://localhost:8080/HealthGridWebService").build();
	  }
	
}
